home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_200
/
204_01
/
decl.c
< prev
next >
Wrap
Text File
|
1980-01-01
|
17KB
|
487 lines
#include <stdio.h>
#include "c.h"
#include "expr.h"
#include "gen.h"
#include "cglbdec.h"
/*
* 68000 C compiler
*
* Copyright 1984, 1985, 1986 Matthew Brandt.
* all commercial rights reserved.
*
* This compiler is intended as an instructive tool for personal use. Any
* use for profit without the written consent of the author is prohibited.
*
* This compiler may be distributed freely for non-commercial use as long
* as this notice stays intact. Please forward any enhancements or questions
* to:
*
* Matthew Brandt
* Box 920337
* Norcross, Ga 30092
*/
TYP *head = 0;
TYP *tail = 0;
char *declid = 0;
TABLE tagtable = {0,0};
TYP stdconst = { bt_long, 1, 4, {0, 0}, 0, "stdconst"};
int imax(i,j)
int i,j;
{ return (i > j) ? i : j;
}
char *litlate(s)
char *s;
{ char *p;
p = xalloc(strlen(s) + 1);
strcpy(p,s);
return p;
}
TYP *maketype(bt,siz)
int bt, siz;
{ TYP *tp;
tp = xalloc(sizeof(TYP));
tp->val_flag = 0;
tp->size = siz;
tp->type = bt;
tp->sname = 0;
tp->lst.head = 0;
return tp;
}
int decl(table)
TABLE *table;
{ switch (lastst) {
case kw_char:
head = tail = maketype(bt_char,1);
getsym();
break;
case kw_short:
head = tail = maketype(bt_short,2);
getsym();
break;
case kw_int: case kw_long:
head = tail = maketype(bt_long,4);
getsym();
break;
case kw_unsigned:
head = tail = maketype(bt_unsigned,4);
getsym();
if( lastst == kw_int )
getsym();
break;
case id: /* no type declarator */
head = tail = maketype(bt_long,4);
break;
case kw_float:
head = tail = maketype(bt_float,4);
getsym();
break;
case kw_double:
head = tail = maketype(bt_double,8);
getsym();
break;
case kw_enum:
getsym();
declenum(table);
break;
case kw_struct:
getsym();
declstruct(bt_struct);
break;
case kw_union:
getsym();
declstruct(bt_union);
break;
}
}
decl1()
{ TYP *temp1, *temp2, *temp3, *temp4;
switch (lastst) {
case id:
declid = litlate(lastid);
getsym();
decl2();
break;
case star:
temp1 = maketype(bt_pointer,4);
temp1->btp = head;
head = temp1;
if(tail == NULL)
tail = head;
getsym();
decl1();
break;
case openpa:
getsym();
temp1 = head;
temp2 = tail;
head = tail = NULL;
decl1();
needpunc(closepa);
temp3 = head;
temp4 = tail;
head = temp1;
tail = temp2;
decl2();
temp4->btp = head;
if(temp4->type == bt_pointer &&
temp4->val_flag != 0 && head != NULL)
temp4->size *= head->size;
head = temp3;
break;
default:
decl2();
break;
}
}
decl2()
{ TYP *temp1;
switch (lastst) {
case openbr:
getsym();
temp1 = maketype(bt_pointer,0);
temp1->val_flag = 1;
temp1->btp = head;
if(lastst == closebr) {
temp1->size = 0;
getsym();
}
else if(head != NULL) {
temp1->size = intexpr() * head->size;
needpunc(closebr);
}
else {
temp1->size = intexpr();
needpunc(closebr);
}
head = temp1;
if( tail == NULL)
tail = head;
decl2();
break;
case openpa:
getsym();
temp1 = maketype(bt_func,0);
temp1->val_flag = 1;
temp1->btp = head;
head = temp1;
if( lastst == closepa) {
getsym();
if(lastst == begin)
temp1->type = bt_ifunc;
}
else
temp1->type = bt_ifunc;
break;
}
}
int alignment(tp)
TYP *tp;
{ switch(tp->type) {
case bt_char: return AL_CHAR;
case bt_short: return AL_SHORT;
case bt_long: return AL_LONG;
case bt_enum: return AL_SHORT;
case bt_pointer:
if(tp->val_flag)
return alignment(tp->btp);
else
return AL_POINTER;
case bt_float: return AL_FLOAT;
case bt_double: return AL_DOUBLE;
case bt_struct:
case bt_union: return AL_STRUCT;
default: return AL_CHAR;
}
}
int declare(table,al,ilc,ztype)
/*
* process declarations of the form:
*
* <type> <decl>, <decl>...;
*
* leaves the declarations in the symbol table pointed to by
* table and returns the number of bytes declared. al is the
* allocation type to assign, ilc is the initial location
* counter. if al is sc_member then no initialization will
* be processed. ztype should be bt_struct for normal and in
* structure declarations and sc_union for in union declarations.
*/
TABLE *table;
int al;
int ilc;
int ztype;
{ SYM *sp, *sp1;
TYP *dhead;
int nbytes;
nbytes = 0;
decl(table);
dhead = head;
for(;;) {
declid = 0;
decl1();
if( declid != 0) { /* otherwise just struct tag... */
sp = xalloc(sizeof(SYM));
sp->name = declid;
sp->storage_class = al;
while( (ilc + nbytes) % alignment(head)) {
if( al != sc_member &&